# -*- coding: utf-8 -*-

import sys
import logging
import json
from logging.handlers import RotatingFileHandler
import subprocess
import os

path = sys.path[0].split('/')
path = "/".join(path[0:-1])
sys.path.append(path)

from mongo_db import DB
from tokenizer import QDTokenizer
from job import write_job

module_path = os.path.split(os.path.realpath(__file__))[0]

def get_data_dir():
    return os.path.join(module_path, "../data")

def train(app, db_name, job_id):
    print("开始处理数据...")
    db_inst = DB()
    db_inst.open("127.0.0.1", "10025")

    write_job(db_inst.database(db_name), job_id, "", "开始处理处理", "")
    inst = QDTokenizer(db_inst.database(db_name), job_id)
    inst.open()
    inst.run()
    inst.close()
    print("数据处理完成")

    print("model train")
    # 三个参数：分别返回1.父目录 2.所有文件夹名字（不含路径） 3.所有文件名字
    for parent, dirnames, filenames in os.walk(get_data_dir()):
        for filename in filenames:
            train_filename = os.path.join(parent, filename)
            ext = os.path.splitext(filename)[1]
            if ext != '.txt' or filename.find("train") == -1:
                continue
            train_filename = os.path.join(module_path, train_filename)

            tran_model = os.path.join(
                module_path, "../data/" + os.path.splitext(filename)[0] + "_model")
            cmds = ["fasttext", "supervised", "-input",
                    train_filename, "-output", tran_model]
            resp = subprocess.Popen(cmds, shell=False)
            ret = resp.wait()
            print("train ", filename, ret)

    print(os.path.isfile(os.path.join(get_data_dir(), "fasttext_train_model.bin")))
    test_file = os.path.join(module_path, "../data/fasttext_test.txt")
    cmds = ["fasttext", "test",  os.path.join(get_data_dir(), "fasttext_train_model.bin"), test_file, "1"]
    resp = subprocess.Popen(cmds, shell=False)
    msg = resp.wait()


if __name__ == "__main__":
    train()
